package com.lrkj.back.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lrkj.back.ChineseToEngLetterUtil;
import com.lrkj.back.Const;
import com.lrkj.back.entity.TbDict;
import com.lrkj.back.service.TbDictService;
import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/dict")// 访问的前缀
public class TbDictController extends BaseController{
	@Autowired
	private TbDictService tbDictService;

	//配置--字典管理
	@RequestMapping("/dictList.do")
	public ModelAndView dictList(String searchTxt,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<String,Object>();
		if(StringUtils.isNotBlank(searchTxt)){
			map.put("dictName", searchTxt);
			map.put("dictCode", searchTxt);
		}
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<TbDict> page = tbDictService.queryTbDictList(map);
		request.setAttribute("tdList", page.getList());
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages()); 
		return new ModelAndView("/peizhi/dict/dict_list");
	}
	
	@RequestMapping("/dictEdit.do")
	public ModelAndView dictEdit(Integer id, HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		TbDict td = (id == null) ? new TbDict() : tbDictService.selectByPrimaryKey(id);
		request.setAttribute("td", td);
		return new ModelAndView("/peizhi/dict/dict_edit");
	}
	
	@RequestMapping(value="/dictSave.do")
	@ResponseBody
	public Map<Object,Object> dictSave(TbDict td,HttpServletRequest request,Model model,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		//新增
		try {
			SimpleDateFormat sdf= new SimpleDateFormat("ddHH");
			String dictCode = "";
			if(td.getDictId() == null){
				dictCode = getMathStr()+"_"+ChineseToEngLetterUtil.getAllFirstLetter(td.getDictName()).toUpperCase();
				TbDict entityDic = tbDictService.selectByCode(td.getDictCode());
				if(entityDic != null){
					dictCode = dictCode+sdf.format(new Date());
				}
				td.setDictCreatetime(new Date());
				td.setDictCode(dictCode);
				tbDictService.insert(td);
			}else{
				tbDictService.updateByPrimaryKeySelective(td);
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功"); 
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping(value="/dictDelete.do")
	@ResponseBody
	public Map<Object,Object>  dictDelete(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		try {
			String ids = request.getParameter("id");
			String[] arraId = ids.split(",");
			for(String tid : arraId){
				tbDictService.deleteByPrimaryKey(Integer.valueOf(tid));
				tbDictService.deleteChildNodes(Integer.valueOf(tid));
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "删除成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	/////////////////////////////////////////////////////////////////////////////////////////
	//
	@RequestMapping
	public void main(){

	}

	
	@RequestMapping(value="/dictSubitemsList.do")
	public ModelAndView dictSubitemsList(Integer id,String searchTxt,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<>();
		if(StringUtils.isNotBlank(searchTxt)){
			map.put("dictName", searchTxt);
			map.put("dictCode", searchTxt);
		}
		map.put("dictParentid", id);
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<TbDict> page = tbDictService.findDictSubitems(map);
		request.setAttribute("tdList", page.getList());
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages());
		request.setAttribute("parentName", tbDictService.selectByPrimaryKey(id).getDictName());
		request.setAttribute("dictParentid", id);
		return new ModelAndView("/peizhi/dict/dict_data_list");
	}
	
	@RequestMapping("/dictSubitemsEdit.do")
	public ModelAndView dictSubitemsEdit(Integer id, HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		TbDict td = (id == null) ? new TbDict() : tbDictService.selectByPrimaryKey(id);
		request.setAttribute("td", td);
		String parentId = request.getParameter("pid");
		request.setAttribute("parentId", parentId);
		return new ModelAndView("/peizhi/dict/dict_data_edit");
	}
	
	@RequestMapping(value="/dictSubitemsDelete.do")
	@ResponseBody
	public Map<Object,Object>  dictSubitemsDelete(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<>();
		try {
			String ids = request.getParameter("id");
			String[] arraId = ids.split(",");
			for(String tid : arraId){
				tbDictService.deleteByPrimaryKey(Integer.valueOf(tid));
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "删除成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	
	 public static String getMathStr()throws BusinessException{
		  StringBuffer sb = new StringBuffer("");
		  String chars = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";
		  for(int i= 0;i<8 ;i++){
			  char a = chars.charAt((int)(Math.random() * 26));
			  sb.append(a);
		  }
		  return sb.toString();
	  }

}
